package com.summit.exam.servlet;

import com.summit.exam.dbdao.StudentDao;
import com.summit.exam.dbdao.UserInfoDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class uNameAndSexServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req , HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        HttpSession session = req.getSession(true);
        String getId = (String) session.getAttribute("username");
        String getName = req.getParameter("name");
        String getSex = req.getParameter("sex");


        StudentDao studentService = new StudentDao();
        UserInfoDao userInfoDao = new UserInfoDao();
        PrintWriter pw = resp.getWriter();

        Boolean result = studentService.updateNameAndSex(getId,getName,getSex);
        Boolean ok = userInfoDao.updateNameAndSex(getId,getName,getSex);

        if(!result && !ok){
            pw.println("<script language='javascript'>alert('修改失败')</script>");
            pw.println("<script>window.history.go(-1)</script>");
        }else {
            req.getSession().setAttribute("ipt_name", getName);
            pw.println("<script language='javascript'>alert('修改成功')</script>");
            pw.println("<script>window.history.go(-1)</script>");

        }
    }
}
